package com.example.graduatetest.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.example.graduatetest.R;
import com.example.graduatetest.activitys.ClassDetailActivity;
import com.example.graduatetest.bean.Classes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ViewHolder> {
    private List<Classes> classesList;
    private Activity activity;
    private final String[] colors = {"黑色", "蓝色","黄色","红色","绿色"};
    private final int[] colorFonts = {Color.BLACK,Color.BLUE,Color.YELLOW,Color.RED,Color.GREEN};
    private Map<String,Integer> map;
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_class_name;
        TextView tv_class_id;
        TextView tv_class_teacher;
        TextView tv_class_number;
        TextView tv_class_time;
        ConstraintLayout constraintLayout;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_class_id = (TextView)itemView.findViewById(R.id.tv_class_id);
            tv_class_name = (TextView)itemView.findViewById(R.id.tv_class_name);
            tv_class_teacher = (TextView)itemView.findViewById(R.id.tv_class_teacher);
            tv_class_number = (TextView)itemView.findViewById(R.id.tv_class_number);
            tv_class_time = (TextView)itemView.findViewById(R.id.tv_class_time);
            constraintLayout = (ConstraintLayout)itemView.findViewById(R.id.class_item_view);
        }
    }

    public ClassAdapter(List<Classes> classesList, Activity activity) {
        super();
        this.classesList = classesList;
        this.activity = activity;
    }

    //初始化颜色map
    private void initColor(){
        map = new HashMap<>();
        for (int i = 0; i <colors.length ; i++) {
            map.put(colors[i],colorFonts[i]);
        }
    }

    //重写onCreateViewHolder方法
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //初始化Item的背景颜色
        initColor();
        //获取View实体，绑定View布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.classes_item, parent,false);
        //返回ViewHolder
        return  new ViewHolder(view);
    }

    //重写onBindViewHolder方法，主要是UI的操作
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Classes classes = classesList.get(position);
        //颜色的设置，默认为绿色
        int color = map.get(classes.getColor()) == null ? Color.GREEN:map.get(classes.getColor());
        holder.constraintLayout.setBackgroundColor(color);//设置item颜色背景
        holder.tv_class_time.setText(classes.getTime());//设置学期内容
        holder.tv_class_number.setText("人数:"+classes.getNumber());//设置班级人数内容
        holder.tv_class_teacher.setText("老师:"+classes.getTeacher());//设置任课老师名称
        holder.tv_class_name.setText(classes.getName());//设置课程名称
        holder.tv_class_id.setText(classes.getId());//设置课程号
        //设置item的点击事件，Activity的跳转
        holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity.getApplication(), ClassDetailActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("classes", classes);
                activity.getApplication().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return classesList == null ? 0:classesList.size();
    }
}
